/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */

/* 
 * Hum -- The Low Calorie Music Manager
 * 
 * Copyright (C) 2007 by Brian Davis <brian.william.davis@gmail.com>
 *
 * This file is modifiable/redistributable under the terms of the GNU
 * General Public License.
 *
 * You should have recieved a copy of the GNU General Public License along
 * with this program; see the file COPYING. If not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 0211-1307, USA.
 */

#include "hum_player.h"

void
hum_init_player ()
{
	hum_debug (__func__, "Initializing Gstreamer...\n");
	gst_init (NULL, NULL);
	
	/* Create a pipeline that will play songs of any type (with an installed codec). */
	hum_player.pipeline = gst_element_factory_make ("playbin", "source");
	
	/* Create, hook up and listen on the bus. */
	hum_player.bus = gst_pipeline_get_bus (GST_PIPELINE (hum_player.pipeline));	
	gst_bus_add_signal_watch (hum_player.bus);
	
	hum_debug (__func__, "Finished initializing Gstreamer.\n");
}

void
hum_destroy_player ()
{
	
}

void 
hum_start_playback (gchar* uri)
{
	hum_stop_playback ();
	hum_player.playing = TRUE;
	
	if (uri)
	{
		hum_player.uri = uri;
	}
	
	g_object_set (G_OBJECT (hum_player.pipeline), "uri", hum_player.uri, NULL);	
	gst_element_set_state (GST_ELEMENT (hum_player.pipeline), GST_STATE_PLAYING);
	
	hum_debug (__func__, "Playback started.\n");
}

void
hum_pause_playback ()
{
	gst_element_set_state (GST_ELEMENT (hum_player.pipeline), GST_STATE_PAUSED);
	
	hum_debug (__func__, "Playback paused.\n");
}

void
hum_resume_playback ()
{
	gst_element_set_state (GST_ELEMENT (hum_player.pipeline), GST_STATE_PLAYING);
	
	hum_debug (__func__, "Playback resumed.\n");
}

void
hum_stop_playback ()
{
	hum_player.playing = FALSE;
	
	gst_element_set_state (GST_ELEMENT (hum_player.pipeline), GST_STATE_READY);
	
	hum_debug (__func__, "Playback stopped.\n");
}

void
hum_seek_playback (gdouble fraction)
{
	GstFormat fmt = GST_FORMAT_TIME;
	gint64 seek_to, total; 
	
	gst_element_query_duration (hum_player.pipeline, &fmt, &total);
	
	seek_to = (gint64) (fraction * (gdouble) total);
	
	if (!gst_element_seek (hum_player.pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH, GST_SEEK_TYPE_SET, seek_to, GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE)) 
	{
  		hum_debug (__func__, "Seek failed.\n");
	}
}

